﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using TSL.BLL;

namespace TSL.OnLines
{
    /// <summary>
    /// PassPort 存储在线用户列表。
    /// </summary>
    public class PassPort
    {
        private int _refreshTimeout;
        private int _activeTimeout;

        public PassPort()
        {
            //活动超时时间初始化 单位：分钟
            try { _activeTimeout = int.Parse(ConfigurationManager.AppSettings["ActiveTimeout"]); }
            catch { _activeTimeout = 120; }   

            //自动刷新超时时间初始化 单位：秒
            try { _refreshTimeout = int.Parse(ConfigurationManager.AppSettings["RefreshTimeout"]); }
            catch { _refreshTimeout = 60; }
        }

        //全部用户列表
        public List<TSL.Model.OnLine> ActiveUsers
        {
            get
            {
                return new TSL.BLL.OnLine().GetModelList(string.Empty);
            }
        }

        /// <summary>
        /// 新用户登陆。
        /// </summary>
        public void Login(ActiveUser user)
        {
            DelTimeOut();  //清除超时用户
            TSL.Model.OnLine online;
            TSL.BLL.OnLine onlinebll= new TSL.BLL.OnLine();
            try
            {
                if (onlinebll.GetModel(user.UserName) != null)
                {
                    online = new TSL.Model.OnLine();
                    online.UserName = user.UserName.Trim();
                    online.RefreshTime = DateTime.Now;
                    online.ClientIP = user.ClientIP.Trim();
                    onlinebll.Update(online);
                }
                else
                {
                    online = new TSL.Model.OnLine();
                    online.UserName = user.UserName.Trim();
                    online.RefreshTime = DateTime.Now;
                    online.ClientIP = user.ClientIP.Trim();
                    onlinebll.Add(online);
                }
            }
            catch (Exception e)
            {
                throw (new Exception(e.Message));
            }
        }

        /// <summary>
        ///用户注销
        /// </summary> 
        private void Logout(ActiveUser user)
        {
            DelTimeOut();  //清除超时用户
            try
            {
                new TSL.BLL.OnLine().Delete(user.UserName);//   清除本用户在线列表
            }
            catch
            {
            }
        }

        /// <summary>
        ///清除超时用户。
        /// </summary>
        private bool DelTimeOut()
        {
            try
            {
                new publish().DeleteOnline_ForRefreshTime(_activeTimeout);
                return true;
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        ///更新系统自动刷新时间。
        /// </summary>
        public void RefreshTime(ActiveUser user)
        {
            Login(user);
            DelTimeOut();
        }
    }
}
